<template>
  <fix-area ref="fixArea">
    <div style="width: 100%;height: 60px; display: flex; justify-content: flex-end; align-items: flex-start">
      <el-button type="primary" @click="createVisible = true">添加</el-button>
    </div>
    <div style="flex: 1; min-height: 0; width: 100%">
      <table-block :loading="loading" :data="shortCategoryList">
        <table-column label="排序" width="120px" prop="sort" fixed="left" />
        <table-column label="分类名称" prop="name" fixed="left" />
        <table-column label="分类图标" width="120px" prop="icon" :use-slot="true">
          <template #default="{row}">
            <div style="cursor: pointer" @click="$refs.fixArea.previewImg(row.icon)">
              <cell-media :url="row.icon" />
            </div>
          </template>
        </table-column>
        <table-column label="分类状态" width="120px" prop="status" :use-slot="true">
          <template #default="{row}">
            <el-tag :type="row.status === 0 ? 'success' : 'danger'" size="small">{{ row.status === 0 ? '显示' : '隐藏' }}</el-tag>
          </template>
        </table-column>
        <table-column label="操作" :use-slot="true" width="220px">
          <template #default="{row}">
            <el-button type="warning" size="mini" @click="editShortCategory(row.id)">编辑</el-button>
            <el-button type="danger" size="mini" @click="deleteShortCategory(row.id)">删除</el-button>
            <el-button type="info" size="mini" @click="toShort(row.id)">短剧</el-button>
          </template>
        </table-column>
      </table-block>
    </div>
    <pagination :current-page="form.page" :total="total" @current-change="currentChange" />
    <create v-if="createVisible" :visible="createVisible" @cancel="createVisible = false" @enter="createEnter" />
    <edit v-if="editVisible" :id="id" :visible="editVisible" @cancel="editVisible = false" @enter="editEnter" />
  </fix-area>
</template>
<script>
import { shortCategoryList, deleteShortCategory } from '@/api/short'
import Create from '@/views/short/category/component/Create.vue'
import Edit from '@/views/short/category/component/Edit.vue'

export default {
  name: 'Category',
  components: {
    Create, Edit
  },
  data() {
    return {
      loading: false,
      shortCategoryList: [],
      form: {
        page: 1
      },
      total: 0,
      createVisible: false,
      editVisible: false,
      id: -1
    }
  },
  async mounted() {
    try {
      this.loading = true
      const shortCategoryListRes = await shortCategoryList(this.form)
      this.shortCategoryList = shortCategoryListRes.data.data
      this.total = shortCategoryListRes.data.total
      this.loading = false
    } catch (error) {
      this.$errorMessage()
      this.loading = false
    }
  },
  methods: {
    async currentChange(event) {
      try {
        this.loading = true
        this.form.page = event
        const shortCategoryListRes = await shortCategoryList(this.form)
        this.shortCategoryList = shortCategoryListRes.data.data
        this.total = shortCategoryListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    async deleteShortCategory(id) {
      try {
        await this.$warningConfirm('确认删除')
        try {
          this.loading = true
          await deleteShortCategory({ category_id: id })
          try {
            this.loading = true
            const shortCategoryListRes = await shortCategoryList(this.form)
            this.shortCategoryList = shortCategoryListRes.data.data
            this.total = shortCategoryListRes.data.total
            this.loading = false
          } catch (error) {
            this.$errorMessage()
            this.loading = false
          }
        } catch (error) {
          this.$errorMessage()
          this.loading = false
        }
      } catch (error) {
        console.log('取消删除')
      }
    },
    editShortCategory(id) {
      this.id = id
      this.editVisible = true
    },
    toShort(id) {
      this.$router.push({
        name: 'Short',
        query: { id }
      })
    },
    async editEnter() {
      this.editVisible = false
      try {
        this.loading = true
        const shortCategoryListRes = await shortCategoryList(this.form)
        this.shortCategoryList = shortCategoryListRes.data.data
        this.total = shortCategoryListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    async createEnter() {
      this.createVisible = false
      try {
        this.loading = true
        const shortCategoryListRes = await shortCategoryList(this.form)
        this.shortCategoryList = shortCategoryListRes.data.data
        this.total = shortCategoryListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    }
  }
}
</script>
